﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace YJKArch.DictModule
{
	public partial class DlgAddSubDirectory : System.Windows.Forms.Form
	{
		public DlgAddSubDirectory(DlgDict mForm)
		{
			this.mainForm = mForm;
			this.InitializeComponent();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.tbxName.Text != "" && this.mainForm.tvDirectory.SelectedNode != null)
			{
				if (DictOperation.AddSubDirectory((XmlNode)this.mainForm.tvDirectory.SelectedNode.Tag, this.tbxName.Text, this.mainForm.dictFile.FullName))
				{
					TreeNode selectedNode = this.mainForm.tvDirectory.SelectedNode;
					new DictOperation(this.mainForm.dictFile.FullName).FillTree(this.mainForm.tvDirectory);
					this.mainForm.tvDirectory.SelectedNode = selectedNode;
					MessageBox.Show("添加成功", "盈建科词库");
				}
				else
				{
					MessageBox.Show("添加失败，请检查是否目录重名。", "盈建科词库");
				}
			}
			base.Close();
		}

		public static DlgAddSubDirectory GetInstance(DlgDict main)
		{
			if (DlgAddSubDirectory.dasd == null || DlgAddSubDirectory.dasd.IsDisposed)
			{
				DlgAddSubDirectory.dasd = new DlgAddSubDirectory(main);
				DlgAddSubDirectory.dasd.MdiParent = main.ActiveMdiChild;
			}
			else
			{
				DlgAddSubDirectory.dasd.tbxName.Text = "";
				DlgAddSubDirectory.dasd.mainForm = main;
			}
			return DlgAddSubDirectory.dasd;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void tbxName_TextChanged(object sender, EventArgs e)
		{
			if (this.tbxName.Text.Trim() != "")
			{
				this.btnAdd.Enabled = true;
				return;
			}
			this.btnAdd.Enabled = false;
		}

		private static DlgAddSubDirectory dasd;

		private DlgDict mainForm;
	}
}
